package hc.util

import org.junit.jupiter.api.Test

import hc.Util
import hc.util.ExcelReader
import hc.util.ExcelUtil

@groovy.transform.CompileStatic
class UtilTest {

	String getPath() {
		"src/test/groovy/" + getClass().getPackage().getName().replace('.', '/')
	}

	@Test
	public void readExcelTextAnchor() {
		def table = ExcelUtil.readExcelFixedWidth(getPath() + "/datatmpl.xlsx", "数据源", 3)

		assert table.size() == 10

		assert "数据源" == table[0][0]

		assert "姓名" == table[2][0]

		assert  table[1].size() == 3
	}

	@Test
	public void readExcelCommentAnchor() {
		def table = ExcelUtil.readExcelFixedWidth(getPath() + "/datatmpl.xlsx", "Data Source", 3)

		assert  table.size()> 0

		assert "数据源" == table[0][0]

		assert "姓名" == table[2][0]
	}

	@Test
	public void testReadExcelTableWidthReferRow() {
		String fileName =  getPath() + "/decisiontableSimpleSample.xlsx"
		def table = ExcelUtil.readExcelWidthReferingRow(fileName, '决策表名称', 1)

		assert 12 ==table.size()

		assert 5 == table[1].size()

		assert "青年" == table[4][0]
	}

	public static void main(String[] args) {

		String fileName

		ExcelUtil.readExcelWidthReferingRow(fileName, '决策表名称', 1)

		Object table =ExcelReader.options()
				.withFile(fileName)
				.withAnchorAt('决策表名称')
				.withTableWidthReferToRow(1)
				.build()
				.readTable();


		println table
	}

	@Test
	@groovy.transform.CompileDynamic
	public void Json2File() {
		def filename = getPath()  + "/createdfile.json"

		def json =	Util.jsonReadFromFile(filename)

		json.rversion += 1;

		def destFile = path + "/createdfile.json"

		Util.jsonWriteToFile json,destFile

		def json2 =	Util.jsonReadFromFile(filename)

		assert json.rversion == json2.rversion
		
		//restore to old
		json.rversion -= 1;
		Util.jsonWriteToFile json,destFile
	}
}
